/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Administrator
 */
public class ListFriendScene extends Scene {

    private static int LIST_HEIGHT = 220;
    private int numberFriend;
    private int topBoardInfo;
    private int avatarHeight;
    private int avatarWidth;
    private int numberShow;
    private int startIndex;
    private int indexSelected;
    private boolean isShowMessageBox;
    private boolean isHasRing;
    private boolean isShowAddFriend;
    private boolean isHasFamily;
    private Image topBarImage;
//    private Image transparentBackground;
    private Image bottomBarImage;
    private Image onlineImage;
    private Image offlineImage;
//    private Image boyAvatarImage;
//    private Image girlAvatarImage;
    private Image avatarImage;
    private Image rowSelectedBackground;
    private CustomTextBox messageBox;
    // private PlayerInfo[] listInfos;
    private String[] menuListString = {"Xóa", "Thêm bạn", "Thông tin", "Gửi tin nhắn"};
    private String[] menuListMarry = {"Xóa", "Thêm bạn", "Thông tin", "Gửi tin nhắn", "Cầu hôn"};
    private int friendIndex;

    public ListFriendScene(MainCanvas parent) {
        super(parent);
        initValue();
        initObject();
    }

    private void initValue() {
        indexSelected = 0;
        LIST_HEIGHT = canvasHeight - 70;
        topBoardInfo = canvasHeight / 2 - LIST_HEIGHT / 2;
        numberFriend = gameData.getNumberFriend();
        //is have ring
        isHasRing = false;

        for (int i = 0; i < gameData.numberItemBuy; i++) {
            if (gameData.listItemBuy[i].id == 3) {
                Debug.d("have ring");
                isHasRing = true;
                break;
            }
        }
        if (gameData.userInfoShow.getIsMaried() == 1) {
            isHasFamily = true;
        }
    }

    private void initObject() {
        try {
            initImage();
            messageBox = new CustomTextBox(PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, false, keyPad);
            subMenu.setList(new String[]{"Xóa", "Xem thông tin", "Gửi tin nhắn", "Cầu hôn"});
            numberShow = LIST_HEIGHT / avatarHeight;
            numberShow = (numberShow > numberFriend ? numberFriend : numberShow);
        } catch (IOException ex) {
        }
    }

    private void initImage() throws IOException {
        topBarImage = library.getTopBarImage();
        bottomBarImage = library.getBottomBarImage();
//        transparentBackground = Utils.createTransparentImage(Constants.OPACITY_BLACK, canvasWidth, LIST_HEIGHT);
        onlineImage = Utils.loadImage("online.png");
        offlineImage = Utils.loadImage("offline.png");
        avatarImage = Utils.loadImage("new_avatar.png");
        avatarWidth = avatarImage.getWidth() / 11;
        avatarHeight = avatarImage.getHeight() / 2;
        rowSelectedBackground = Utils.createTransparentImage(Constants.OPACITY_BLACK, canvasWidth, avatarHeight);
//        Debug.d("row :" + rowSelectedBackground);
    }

    public void setListSubMenu() {
    }

    public void doLoop() {
        if ((isShowMessageBox || isShowAddFriend) && countFrame % 4 == 0) {
            messageBox.run();
            parent.repaint();
        }
    }
    //---------------------------------------------------------------------------

    public void paintComponents(Graphics g) {
        paintBarMenu(g);
        if (numberFriend != 0) {
            paintList(g);
        } else {
            PiPoDesigner.drawString(g, "Bạn chưa kết bạn với ai.", PiPoDesigner.COLOR_BLACK,
                    PiPoDesigner.COLOR_WHITE, leftScreen + 40, topBoardInfo + 5);
        }
        if (isShowMessageBox || isShowAddFriend) {
            messageBox.paint(g, leftScreen + 10, canvasHeight - bottomBarImage.getHeight() - 20, canvasWidth - 20, 20);
        }
    }

    private void paintList(Graphics g) {
        g.setClip(leftScreen, topBoardInfo, canvasWidth, LIST_HEIGHT);
        g.setColor(0x0000aa);
        g.setColor(0x00ff00);
        g.drawImage(rowSelectedBackground, leftScreen + 2, topBoardInfo + 5 + avatarHeight * indexSelected, 0);
        int topAvatar = topBoardInfo + 5;
        for (int i = startIndex; i < numberShow + startIndex; i++) {
            PlayerInfo info = gameData.listInfos[i];
            Utils.drawFrame(g, avatarImage, leftScreen + 5, topAvatar, info.avatarId, 0, avatarWidth, avatarHeight);
            Image statusImage = (info.getIsOnline() == 0 ? offlineImage : onlineImage);
            g.drawImage(statusImage, leftScreen + 5 + avatarWidth, topAvatar + 5, 0);
            PiPoDesigner.drawString(g, info.getName(), PiPoDesigner.COLOR_BLACK,
                    PiPoDesigner.COLOR_WHITE, leftScreen + avatarWidth + 20, topAvatar);
            PiPoDesigner.drawString(g, info.getStatus(), PiPoDesigner.COLOR_BLACK,
                    PiPoDesigner.COLOR_GREEN, leftScreen + avatarWidth + 20, topAvatar + 20);
            topAvatar += avatarHeight;
        }
        g.setClip(0, 0, canvasWidth - 2, canvasHeight);
    }

    protected void paintBarMenu(Graphics g) {
        g.drawImage(topBarImage, leftScreen, topScreen, 0);
        g.drawImage(bottomBarImage, leftScreen, canvasHeight - bottomBarImage.getHeight(), 0);
        PiPoDesigner.drawString(g, "Bạn bè", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_YELOW,
                canvasWidth / 2 - PiPoDesigner.stringWidth("ban be") / 2, topScreen + 5);
        if (isShowMessageBox || isShowAddFriend) {
            PiPoDesigner.drawString(g, "Đóng", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, leftScreen + 5, canvasHeight - 25);
            PiPoDesigner.drawString(g, "Xóa", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, canvasWidth - 40, canvasHeight - 25);
            PiPoDesigner.drawString(g, "Gửi", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, canvasWidth / 2 - 15, canvasHeight - 25);
        } else {
            if (isShowSubMenu) {
                PiPoDesigner.drawString(g, "Đóng", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, canvasWidth - 40, canvasHeight - 25);
            } else {
                PiPoDesigner.drawString(g, "Thoát", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, canvasWidth - 40, canvasHeight - 25);
            }
            PiPoDesigner.drawString(g, "Menu", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE, leftScreen + 5, canvasHeight - 25);
        }
    }
    //---------------------------------------------------------------------------

    public void doKeyLeft() {
        if (isShowUserInfo) {
            moveLeftInfo();
            return;
        }
    }

    public void doKeyRight() {
        if (isShowUserInfo) {
            moveRightInfo();
            return;
        }
    }

    public void doKeyPound() {
        if (isShowMessageBox) {
            messageBox.doKeyPound();
            parent.repaint();
        }
    }

    protected void doKeyUpRemain() {
        if (numberFriend <= 0) {
            return;
        }
        indexSelected--;
        if (indexSelected < 0) {
            indexSelected = numberShow - 1;
            numberShow = (LIST_HEIGHT / avatarHeight);
            startIndex -= numberShow;
            if (startIndex < 0) {
                indexSelected = 0;
                startIndex = numberFriend - (numberFriend % numberShow);
                numberShow = numberFriend - startIndex;

            }
        }
        parent.repaint();
    }

    protected void doKeyDownRemain() {
        if (numberFriend <= 0) {
            return;
        }
        indexSelected = (++indexSelected) % numberShow;
        if (indexSelected == 0) {
            startIndex += numberShow;
            indexSelected = 0;
            if (startIndex + numberShow >= numberFriend) {
                numberShow = numberFriend - startIndex;
            }
        }
        if (startIndex + indexSelected == numberFriend) {
            startIndex = 0;
            indexSelected = 0;
            numberShow = LIST_HEIGHT / avatarHeight;
            if (numberShow > numberFriend) {
                numberShow = numberFriend;
            }
        }
        parent.repaint();
    }

    public void doSoftKeyLeft() {

        if (isShowMessageBox || isShowAddFriend) {
            isShowMessageBox = false;
            isShowAddFriend = false;
            parent.repaint();
            return;
        }
        if (numberFriend > 0) {

            if (gameData.listInfos[indexSelected].getGender() != gameData.getMyGender()
                    && gameData.listInfos[indexSelected].getIsMaried() == 0 && !isHasFamily) {
                subMenu.setList(menuListMarry);
            } else {
                subMenu.setList(menuListString);
            }
        } else {
            subMenu.setList(menuListString);
        }
        isShowSubMenu = true;
        subMenu.setVisible(true);
        parent.repaint();
    }

    protected void doSoftKeyLeftRemain() {
    }

    protected void doSoftKeyRightRemain() {
        if (isShowMessageBox || isShowAddFriend) {
            messageBox.doSoftKeyRight();
            parent.repaint();
            return;
        }
        showChooseGame();
    }

    protected void reponseQuestion(int result) {
    }

    protected void doKeyCenterRemain() {
        if (isShowMessageBox) {
            String text = messageBox.getText();
            if (text.length() != 0) {
                Request.sendMessage(0, gameData.listInfos[indexSelected].getName(), messageBox.getText());
                isShowMessageBox = false;
                parent.repaint();
            }
            return;
        }
        if (isShowAddFriend) {
            String text = messageBox.getText();
            if (text.length() != 0) {
                if (text.equals(gameData.getMyName())) {
                    dialogString = "Tên không hợp lệ";
                    isShowAddFriend = false;
                    parent.repaint();
                    return;
                }
                Request.sendMessage(MESSAGE_FRIEND, text, "0");
                isShowAddFriend = false;
                isLoading = true;
                parent.repaint();
            }
            return;
        }
    }

    public void inputKeyCode(int key) {
        if (messageBox.isFocus()) {
            messageBox.inputChar(key);
        }
    }

    protected void touchPressedRemain(int x, int y) {
    }

    public void doTouchReleased(int x, int y) {
    }

    public void doTouchDraged(int x, int y) {
    }

    public void processMessage(String[] part) {
        super.processMessage(part);
        if (part[0].equals("LF")) {
            //LF|name|online|name|online
            if (part[1].equals("1")) {
                friendIndex = 0;
                gameData.initListFriend(Integer.parseInt(part[2]));
                numberFriend = Integer.parseInt(part[2]);
                numberShow = LIST_HEIGHT / avatarHeight;
                numberShow = (numberShow > numberFriend ? numberFriend : numberShow);
                return;
            }
            if (part[1].equals("-1")) {
                initValue();
                isLoading = false;
                isGetListFriend = false;
                parent.repaint();
                return;
            }
            gameData.setFriendInfo(friendIndex, part[1], Integer.parseInt(part[2]), part[3],
                    Integer.parseInt(part[4]), Integer.parseInt(part[5]), Integer.parseInt(part[6]));
            friendIndex++;
        }
        if (part[0].equals("RMF")) {
            int result = Integer.parseInt(part[1]);
            isGetListFriend = false;
            if (result == 0) {
                dialogString = "nick " + part[2] + " khong ton tai";
                isLoading = false;
                parent.repaint();
                return;
            }
            if (result == 1) {
                dialogString = "Đã gửi tin nhắn kết bạn cho :" + part[2];
                isLoading = false;
                parent.repaint();
                return;
            }
            if (result == 2) {
                dialogString = "Bạn đã kết bạn với :" + part[2];
                isLoading = false;
                parent.repaint();
                return;
            }
        }
    }

    protected void doSubMenu() {
        switch (subMenu.getSelectedIndex()) {
            case 0://xoa ban
                if (numberFriend == 0) {
                    dialogString = "Bạn chưa kết bạn với ai.";
                    parent.repaint();
                    break;
                }
                Request.deleteFriend(gameData.getMyName(), gameData.listInfos[indexSelected].getName());
                isLoading = true;
                isGetListFriend = true;
                parent.repaint();
                break;
            case 1://them ban
                messageBox.setText("");
                isGetListFriend = true;
                dialogString = "Hãy nhập nick bạn muốn thêm.";
                isShowAddFriend = true;
                parent.repaint();
                break;
            case 2://thong tin
                if (numberFriend == 0) {
                    dialogString = "Bạn chưa kết bạn với ai.";
                    parent.repaint();
                    break;
                }
                Request.getUserInfo(gameData.listInfos[indexSelected].getName());
                isGetUserInfo = true;
                isLoading = true;
                break;
            case 3:// gui tin nhan
                if (numberFriend == 0) {
                    dialogString = "Bạn chưa kết bạn với ai.";
                    parent.repaint();
                    break;
                }
                isShowMessageBox = true;
                messageBox.setFocus(true);
                messageBox.setText("");
                parent.repaint();

                break;
            case 4:// cau hon
                isShowSubMenu = false;
                if (numberFriend == 0) {
                    dialogString = "Bạn chưa kết bạn với ai.";
                    parent.repaint();
                    return;
                }
                if (gameData.isWaitingMarry) {
                    dialogString = "Bạn đang chờ trả lời cầu hôn.";
                    parent.repaint();
                    return;
                }
                if (!isHasRing) {
                    dialogString = "Bạn cần có nhẫn để kết hôn";
                    parent.repaint();
                    return;
                }
                gameData.isWaitingMarry = true;
                dialogString = "Lời cầu hôn của bạn đã được gửi đi";
                Request.sendMessage(MESSAGE_MARRY, gameData.listInfos[indexSelected].getName(), "");
                parent.repaint();
                break;
        }
        isShowSubMenu = false;
        subMenu.setVisible(false);
        parent.repaint();
    }
    //------------------------------------------------------------------------

    public void dispose() {
        topBarImage = null;
        bottomBarImage = null;
        onlineImage = null;
        offlineImage = null;
        avatarImage = null;
        rowSelectedBackground = null;
        messageBox = null;
//        listInfos = null;
        menuListString = null;
        menuListMarry = null;
        gameData.disposeListInfos();
        super.dispose();
    }
}
